"""
查找并绘制轮廓
"""
import cv2

img = cv2.imread('../data/3.png')
# cv2.imread('img',img)

# 将图像转换为灰度图像
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 对图像进行二值化
t,binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)

# cv2.imshow('binary',binary)

# 查找轮廓
cnts,hie = cv2.findContours(binary,
                                  cv2.RETR_EXTERNAL, # 只检测外层轮廓
                                  cv2.CHAIN_APPROX_NONE) # 保存所有坐标
# print(type(cnts))
# print(cnts)
# print(len(cnts))

# for cnt in cnts:
#     print(cnt.shape)


# 绘制轮廓
cv2.drawContours(img, # 在那个图像上画
                 cnts, # 要绘制的轮廓坐标点
                 -1, # 绘制的轮廓序号，-1为绘制所有
                 (0,0,255), # 线条颜色 BGR
                 2) # 线条的粗细
cv2.imshow('img_cnt',img)

cv2.waitKey()
cv2.destroyAllWindows()